(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.reportCompanyEditController', reportCompanyEditController);
	reportCompanyEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','reportService'];
	function reportCompanyEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService,reportService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("reportCompanyEditLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.reportFormatsOptions = reportService.getReportFormatsOptions();
		ctrl.ok = function() {
			if(ctrl.reportCompanyEdit.reportParms && ctrl.reportCompanyEdit.reportParms.length > 0){
				   for (var i = 0; i < ctrl.reportCompanyEdit.reportParms.length; i++) {
				    	var reportParm = ctrl.reportCompanyEdit.reportParms[i];
				    	if(!reportParm.name && !reportParm.value){
					    	modalService.openErrorDialogs({
					    		messages : ['Name is required.','Value is required.']
							});
					    	return;
					  }else if(!reportParm.name){
					    	modalService.openErrorDialogs({
								messages : [ 'Name is required.' ]
							});
					    	return;
					  }else if(!reportParm.value){
					    	modalService.openErrorDialogs({
								messages : [ 'Value is required.' ]
							});
					    	return;
					  }
				    }
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.reportCompanyEdit) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			
			ctrl.reportCompanyEdit = {};
			if (ctrl.dialogsModule.params.companyRpt) {
				ctrl.reportCompanyEdit = ctrl.dialogsModule.params.companyRpt;// get params from parent page
			} else {
				ctrl.reportCompanyEdit = {};
			}
			reportService.getReportOptions().then(function(response) {
				ctrl.localeCodeOptions = response.local;
			});
			ctrl.reportEditParameterGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.reportParmsCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.reportEditParameterGridOptions.data = 'vm.reportCompanyEdit.reportParms';
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedParameters = selectedRows;
		}
		ctrl.addReportParameter = function(row) {
			return $q(function(resolved, rejected){
				ctrl.reportCompanyEdit.reportParms = ctrl.reportCompanyEdit.reportParms || [];
				ctrl.reportCompanyEdit.reportParms.push({
				});
			});
			
		};


	}
})(angular);
